﻿using System;
using Tao.OpenGl;

namespace GameStructure.Types
{
    public class Circle
    {
        private Color _color = new Color(1,1,1,1);
        private Vector _position;
        private readonly double _radius;

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        public Circle()
        {
            _position = Vector.Zero;
            _radius = 1;
        }

        public Circle(Vector position, double radius)
        {
            _position = position;
            _radius = radius;
        }

        public bool Intersects(Point point)
        {
            // Change point to a vector
            var vPoint = new Vector(point.X, point.Y, 0);
            var vFromCircleToPoint = _position - vPoint;
            var distance = vFromCircleToPoint.Length();

            return !(distance > _radius);
        }

        public void Draw()
        {
            Gl.glColor3f(_color.Red, _color.Green, _color.Blue);

            // Determines how round the circle will appear.
            const int vertexAmount = 10;
            const double twoPI = 2.0*Math.PI;

            // A line loop connects all the vertices with lines
            // The last vertex is connected to the first vertex to make a loop.
            Gl.glBegin(Gl.GL_LINE_LOOP);
            {
                for (var i = 0; i <= vertexAmount; i++)
                {
                    var xPos = _position.X + _radius*Math.Cos(i*twoPI/vertexAmount);
                    var yPos = _position.Y + _radius*Math.Sin(i*twoPI/vertexAmount);
                    Gl.glVertex2d(xPos, yPos);
                }
            }
            Gl.glEnd();
        }
    }
}